﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace editor
{
    /// <summary>
    /// MapControl.xaml 的交互逻辑
    /// </summary>
    public partial class MapControl : UserControl
    {
        private cfg.DMap _data;
        private cfg.TMap _type;
        private cfg.TType _keyType;
        private cfg.TType _valueType;

        public MapControl(cfg.DMap data)
        {
            InitializeComponent();

            this.DataContext = data;

            this._data = data;
            _type = data.DType as cfg.TMap;
            _keyType = _type.KeyType;
            _valueType = _type.ValueType;
        }

        private void Append(object sender, RoutedEventArgs e)
        {
            _data.Add(_keyType.NewDefault(), _valueType.NewDefault());
        }

        private void Copy(object sender, RoutedEventArgs e)
        {
            var item = (KeyValuePair<cfg.DData, cfg.DData>)(sender as MenuItem).DataContext;
            int index = _data.Value.IndexOf(item);
            if (index >= 0)
            {
                _data.Insert(index + 1, item.Key.Clone(), item.Value.Clone());
            }
        }

        private void InsertBefore(object sender, RoutedEventArgs e)
        {
            var item = (KeyValuePair<cfg.DData, cfg.DData>)(sender as MenuItem).DataContext;
            int index = _data.Value.IndexOf(item);
            if (index >= 0)
            {
                _data.Insert(index, _keyType.NewDefault(), _valueType.NewDefault());
            }
        }

        private void InsertAfter(object sender, RoutedEventArgs e)
        {
            var item = (KeyValuePair<cfg.DData, cfg.DData>)(sender as MenuItem).DataContext;
            int index = _data.Value.IndexOf(item);
            if (index >= 0)
            {
                _data.Insert(index + 1, _keyType.NewDefault(), _valueType.NewDefault());
            }
        }

        private void Delete(object sender, RoutedEventArgs e)
        {
            var item = (KeyValuePair<cfg.DData, cfg.DData>)(sender as MenuItem).DataContext;
            int index = _data.Value.IndexOf(item);
            if (index >= 0)
            {
                _data.RemoveAt(index);
            }
        }
    }
}
